package com.crishne.anwo.utill;

import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.ValueFilter;
import com.crishne.anwo.config.ConfigInfo;
import com.crishne.anwo.model.Menu;

public class CommonUtill {
	
	ConfigInfo configInfo = new ConfigInfo();
	
	public void OutputJson(Object object,HttpServletResponse response,HttpServletRequest request) {
		PrintWriter out = null;
		response.setContentType("application/json");
		response.setCharacterEncoding("utf-8");
		String json=null;
		try {
			out = response.getWriter();
			json = JSONObject.toJSONString(object,filter);//JSON.toJSONStringWithDateFormat(object, "yyyy-MM-dd HH:mm:ss");
		} catch (IOException e) {
			e.printStackTrace();
		}
		out.print(json);
		out.close();
	}
	
	private static ValueFilter filter = new ValueFilter() {
        @Override
        public Object process(Object obj, String s, Object v) {
            if (v == null)
                return "";
            return v;
        }
    };
	
	public String ChageMenuListToJson(List<Menu> menulist){
		List<Map> retMapList = new ArrayList();
		//获取第一级菜单
		List<Map> firstMapList =  new ArrayList();
		List<Menu> firstList = new ArrayList();
		for(int i=0;i<menulist.size();i++){
			Menu listItem = menulist.get(i);
			//第一级菜单
			if(listItem.getStPid() == null || "".equals(listItem.getStPid())){
				firstList.add(listItem);
			}
		}
		
		for(int i=0;i<firstList.size();i++){
			Menu listItem = firstList.get(i);
			
			HashMap parentMap = new HashMap();
			parentMap.put("id", listItem.getStMenuId());
			parentMap.put("name", listItem.getStMenuName());
			parentMap.put("url", listItem.getStMenuUrl());
			parentMap.put("img", listItem.getStImage());
			parentMap.put("pid", listItem.getStPid());
			parentMap.put("flag", listItem.getNmFlag());
			
			parentMap.put("childs", GetChildMenu(listItem.getStMenuId(),menulist));
			
			retMapList.add(parentMap);
			
		}
		
		
		return JSON.toJSONStringWithDateFormat(retMapList, "yyyy-MM-dd HH:mm:ss");
	}
	
	public List<Map> GetChildMenu(String stParentId,List<Menu> menulist){
		List<Map> retMapList = new ArrayList();
		
		for(int i=0;i<menulist.size();i++){
			HashMap childMap = new HashMap();
			Menu listItem = menulist.get(i);
			if(listItem.getStPid().equals(stParentId)){
				childMap.put("id", listItem.getStMenuId());
				childMap.put("name", listItem.getStMenuName());
				childMap.put("url", listItem.getStMenuUrl());
				childMap.put("img", listItem.getStImage());
				childMap.put("pid", listItem.getStPid());
				childMap.put("flag", listItem.getNmFlag());
				
				childMap.put("childs", GetChildMenu(listItem.getStMenuId(),menulist));
				
				retMapList.add(childMap);
			}
		}
		return retMapList;
		
	}
	
	/**
	 * 生成订单编号
	 * @return
	 */
	public String genOrderNo(){
		Calendar nowtime = new GregorianCalendar();
		//年月日时分秒 毫秒
		String strOrderNo=String.format("%04d", nowtime.get(Calendar.YEAR))+
                String.format("%02d", nowtime.get(Calendar.MONTH)+1)+ 
                String.format("%02d", nowtime.get(Calendar.DATE))+ 
                String.format("%02d", nowtime.get(Calendar.HOUR_OF_DAY))+ 
                String.format("%02d", nowtime.get(Calendar.MINUTE))+ 
                String.format("%02d", nowtime.get(Calendar.SECOND))+
                String.format("%03d", nowtime.get(Calendar.MILLISECOND));
		
		return strOrderNo;
	}
	
	/**
	 * 生成支付编号
	 * @return
	 */
	public String genZhifuNo(){
		Calendar nowtime = new GregorianCalendar();
		//年月日时分秒 毫秒
		String strOrderNo="ZF"+String.format("%04d", nowtime.get(Calendar.YEAR))+
                String.format("%02d", nowtime.get(Calendar.MONTH)+1)+ 
                String.format("%02d", nowtime.get(Calendar.DATE))+ 
                String.format("%02d", nowtime.get(Calendar.HOUR_OF_DAY))+ 
                String.format("%02d", nowtime.get(Calendar.MINUTE))+ 
                String.format("%02d", nowtime.get(Calendar.SECOND))+
                String.format("%03d", nowtime.get(Calendar.MILLISECOND));
		
		return strOrderNo;
	}
	

	public static void main(String[] args){ 
		   double i=2, j=2.1, k=2.5, m=2.9; 
		   System.out.println("舍掉小数取整:Math.floor(2)=" + (int)Math.floor(i)); 
		   System.out.println("舍掉小数取整:Math.floor(2.1)=" + (int)Math.floor(j)); 
		   System.out.println("舍掉小数取整:Math.floor(2.5)=" + (int)Math.floor(k)); 
		   System.out.println("舍掉小数取整:Math.floor(2.9)=" + (int)Math.floor(m)); 
		                                        
		   
		   System.out.println("四舍五入取整:(2)=" + new BigDecimal("2").setScale(0, BigDecimal.ROUND_HALF_UP)); 
		   System.out.println("四舍五入取整:(2.1)=" + new BigDecimal("2.1").setScale(0, BigDecimal.ROUND_HALF_UP)); 
		   System.out.println("四舍五入取整:(2.5)=" + new BigDecimal("2.5").setScale(0, BigDecimal.ROUND_HALF_UP)); 
		   System.out.println("四舍五入取整:(2.9)=" + new BigDecimal("2.9").setScale(0, BigDecimal.ROUND_HALF_UP));
		   System.out.println("凑整:Math.ceil(2)=" + (int)Math.ceil(i)); 
		   System.out.println("凑整:Math.ceil(2.1)=" + (int)Math.ceil(j)); 
		   System.out.println("凑整:Math.ceil(2.5)=" + (int)Math.ceil(k)); 
		   System.out.println("凑整:Math.ceil(2.9)=" + (int)Math.ceil(m));
		   System.out.println("舍掉小数取整:Math.floor(-2)=" + (int)Math.floor(-i)); 
		   System.out.println("舍掉小数取整:Math.floor(-2.1)=" + (int)Math.floor(-j)); 
		   System.out.println("舍掉小数取整:Math.floor(-2.5)=" + (int)Math.floor(-k)); 
		   System.out.println("舍掉小数取整:Math.floor(-2.9)=" + (int)Math.floor(-m)); 
		   
		   System.out.println("四舍五入取整:(-2)=" + new BigDecimal("-2").setScale(0, BigDecimal.ROUND_HALF_UP)); 
		   System.out.println("四舍五入取整:(-2.1)=" + new BigDecimal("-2.1").setScale(0, BigDecimal.ROUND_HALF_UP)); 
		   System.out.println("四舍五入取整:(-2.5)=" + new BigDecimal("-2.5").setScale(0, BigDecimal.ROUND_HALF_UP)); 
		   System.out.println("四舍五入取整:(-2.9)=" + new BigDecimal("-2.9").setScale(0, BigDecimal.ROUND_HALF_UP));
		   System.out.println("凑整:Math.ceil(-2)=" + (int)Math.ceil(-i)); 
		   System.out.println("凑整:Math.ceil(-2.1)=" + (int)Math.ceil(-j)); 
		   System.out.println("凑整:Math.ceil(-2.5)=" + (int)Math.ceil(-k)); 
		   System.out.println("凑整:Math.ceil(-2.9)=" + (int)Math.ceil(-m)); 
		
		BigDecimal big1 = new BigDecimal("10");
        BigDecimal big2 = new BigDecimal("3");
        BigDecimal remainder = big1.remainder(big2);
        System.out.println("5除以2，求余："+remainder.toPlainString());
        
        BigDecimal remainder1 = big1.divideAndRemainder(big2)[0];
        System.out.println("5除以2，求整："+remainder1.toPlainString());
        
        BigDecimal remainder2 = big1.divideAndRemainder(big2)[1];
        System.out.println("5除以2，求余："+remainder2.toPlainString());
        
        BigDecimal remainder3 = big1.divide(big2, 2,BigDecimal.ROUND_HALF_UP);
        System.out.println("5除以2，求大整："+Math.floor(remainder3.doubleValue()));
	} 
}
